package com.efast.cafe.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/******************************************************************************
 * @File name   :      ObjectsTranscoder.java
 *
 * @Author      :      fxm
 *
 * @Date        :      2016-7-7
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-7-7 上午8:45:49        fxm     1.0            Initial Version
 *****************************************************************************/
public class ObjectsTranscoder<T> {

	private static  Logger logger = LoggerFactory.getLogger(ObjectsTranscoder.class);
	
	/**
	 * list转 byte
	 * @Date        :      2016-7-7
	 * @param value
	 * @return
	 */
	public static byte[] serializeList(List<Object> value) {  
        if (value == null) {  
            throw new NullPointerException("Can't serialize null");  
        }  
        byte[] rv=null;  
        ByteArrayOutputStream bos = null;  
        ObjectOutputStream os = null;  
        try {  
            bos = new ByteArrayOutputStream();  
            os = new ObjectOutputStream(bos);  
            for(Object obj : value){  
                os.writeObject(obj);  
            }  
            os.writeObject(null);  
            os.close();  
            bos.close();  
            rv = bos.toByteArray();  
        } catch (IOException e) {  
            throw new IllegalArgumentException("Non-serializable object", e);  
        } finally {  
            close(os);  
            close(bos);  
        }  
        return rv;  
    }  
	
	public static void close(Closeable closeable) {  
        if (closeable != null) {  
            try {  
                closeable.close();  
            } catch (Exception e) {  
                logger.info("Unable to close %s", e);  
            }  
        }  
    } 

	/**
	 * byte转list
	 * @Date        :      2016-7-7
	 * @param in
	 * @return
	 */
    public static <T> List<Object> deserializeList(byte[] in) {  
        List<Object> list = new ArrayList<Object>();  
        ByteArrayInputStream bis = null;  
        ObjectInputStream is = null;  
        try {  
            if(in != null) {  
                bis=new ByteArrayInputStream(in);  
                is=new ObjectInputStream(bis);  
                while (true) {  
                    Object obj = is.readObject();  
                    if(obj == null){  
                        break;  
                    }else{  
                        list.add(obj);  
                    }  
                }  
            }  
        } catch (IOException e) {  
            logger.warn("Caught IOException decoding %d bytes of data", e);  
        } catch (ClassNotFoundException e) {  
            logger.warn("Caught CNFE decoding %d bytes of data", e);  
        } finally {  
        	
    		try {
    			if(null!=is){
    				is.close();
    			}
    			if(null!=bis){
            		bis.close();
            	}
			} catch (IOException e) {
				logger.error("IO Exception by deserialize", e);
			}
        	
        	
        }  
        return list;  
    }  
  
  
   /**
    * object转byte
    * @Date        :      2016-7-7
    * @param value
    * @return
    */
    public static byte[] serializeObject(Object value) {  
        if (value == null) {  
            throw new NullPointerException("Can't serialize null");  
        }  
        byte[] rv=null;  
        ByteArrayOutputStream bos = null;  
        ObjectOutputStream os = null;  
        try {  
            bos = new ByteArrayOutputStream();  
            os = new ObjectOutputStream(bos);  
            os.writeObject(value);  
            os.close();  
            bos.close();  
            rv = bos.toByteArray();  
        } catch (IOException e) {  
            throw new IllegalArgumentException("Non-serializable object", e);  
        } finally {  
            close(os);  
            close(bos);  
        }  
        return rv;  
    }  

    /**
     * byte转object
     * @Date        :      2016-7-7
     * @param in
     * @return
     */
    public static Object deserializeObject(byte[] in) {  
        Object rv=null;  
        ByteArrayInputStream bis = null;  
        ObjectInputStream is = null;  
        try {  
            if(in != null) {  
                bis=new ByteArrayInputStream(in);  
                is=new ObjectInputStream(bis);  
                rv=is.readObject();  
            }  
        } catch (IOException e) {  
            logger.warn("Caught IOException decoding %d bytes of data", e);  
        } catch (ClassNotFoundException e) {  
            logger.warn("Caught CNFE decoding %d bytes of data", e);  
        } finally {  
        	try {
    			if(null!=is){
    				is.close();
    			}
    			if(null!=bis){
            		bis.close();
            	}
			} catch (IOException e) {
				logger.error("IO Exception by deserializeObject", e);
			}  
        }  
        return rv;  
    }

}